package com.keiou.drink.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.keiou.drink.R;
import com.keiou.drink.api.AbsSubscriber;
import com.keiou.drink.api.exception.ApiException;
import com.keiou.drink.api.remote.Api;
import com.keiou.drink.api.rx.RxHelper;
import com.keiou.drink.request.Pwd;
import com.keiou.drink.response.base.HttpResult;
import com.keiou.drink.ui.activity.base.RxActivity;
import com.keiou.drink.util.CToast;
import com.keiou.drink.util.PrefsHelper;
import com.keiou.drink.util.UIUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者 ：Bob on 2017/10/30.
 * 邮箱 ：motherlovefather@126.com
 */

public class LoginActivity extends RxActivity {
    @BindView(R.id.ed_pwd)
    EditText edPwd;
    @BindView(R.id.iv_login)
    ImageView ivLogin;
    @BindView(R.id.ly_psd)
    LinearLayout lyPsd;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

    }

    @Override
    protected void initData() {
    }

    @Override
    protected void loadData() {

    }

    @OnClick({R.id.iv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_login:
                login();
                break;
        }
    }


    private void login() {
        String pwd = edPwd.getText().toString();
        if (TextUtils.isEmpty(pwd)) {
            CToast.showShort(this, "密码不能为空");
            return;
        }

        showLoadingDialog();
        addSubscribe(Api.getInstance().login(PrefsHelper.getDeviceId(this), pwd)
                .compose(RxHelper.applySchedulers())
                .subscribe(new AbsSubscriber<HttpResult<Pwd>>() {
                    @Override
                    public void onNext(HttpResult<Pwd> result) {
                        hideWaitDialog();
                        redirectToOrderHistory();
                    }


                    @Override
                    protected void onError(ApiException ex) {
                        hideWaitDialog();
                        CToast.showShort(LoginActivity.this, ex.getMsg());
                    }

                    @Override
                    protected void onIntercept(int code) {
                        onInterceptCode(code);
                    }
                }));

    }

    private void redirectToOrderHistory() {
        UIUtil.redirectTo(this, OrderHistoryActivity.class);
        finish();
    }
}
